home *** CD-ROM | disk | FTP | other *** search
/ Aminet 2 / Aminet AMIGA CDROM (1994)(Walnut Creek)[Feb 1994][W.O. 44790-1].iso / Aminet / os30 / gfx / animdemo.lha / audio.c < prev    next >
C/C++ Source or Header  |  1993-01-22  |  5KB  |  125 lines

  1. /*******************************************************************************
  2.  *
  3.  * (c) Copyright 1993 Commodore-Amiga, Inc.  All rights reserved.
  4.  *
  5.  * This software is provided as-is and is subject to change; no warranties
  6.  * are made.  All use is at your own risk.  No liability or responsibility
  7.  * is assumed.
  8.  *
  9.  * audio.c - uses the audio.device to initialise and play all the audio.
  10.  *
  11.  ******************************************************************************/
  12.  
  13. #include <exec/execbase.h>
  14. #include <graphics/gfxbase.h>
  15. #include <devices/audio.h>
  16. #include <proto/exec.h>
  17. #include "stick.h"
  18. #include "playanim.h"
  19.  
  20. extern struct GfxBase *GfxBase;
  21. extern struct ExecBase *SysBase;
  22.  
  23. void ThrustAudio(struct IOAudio *ThrustAudioIO[], struct RawAudio *RawAudio, UBYTE *ThrustBuffer)
  24. {
  25.     ThrustAudioIO[0]->ioa_Request.io_Command = CMD_WRITE;
  26.     ThrustAudioIO[0]->ioa_Request.io_Flags = ADIOF_PERVOL;
  27.     ThrustAudioIO[0]->ioa_Data = ThrustBuffer;
  28.     ThrustAudioIO[0]->ioa_Length = RawAudio->ra_SizeOfSample;
  29.     ThrustAudioIO[0]->ioa_Period = PERIOD(RawAudio->ra_SamplesPerSecond);
  30.     ThrustAudioIO[0]->ioa_Volume = VOLUME(RawAudio->ra_Volume);
  31.     ThrustAudioIO[0]->ioa_Cycles = 0;    /* infinite */
  32.  
  33.     ThrustAudioIO[1]->ioa_Request.io_Command = CMD_WRITE;
  34.     ThrustAudioIO[1]->ioa_Request.io_Flags = ADIOF_PERVOL;
  35.     ThrustAudioIO[1]->ioa_Data = ThrustBuffer;
  36.     ThrustAudioIO[1]->ioa_Length = RawAudio->ra_SizeOfSample;
  37.  
  38.     /* For stereo effect, modify the period of the other channel slightly. This
  39.      * number just happens to be good for the audio I used.
  40.      */
  41.  
  42.     ThrustAudioIO[1]->ioa_Period = (PERIOD(RawAudio->ra_SamplesPerSecond) - 27);
  43.     ThrustAudioIO[1]->ioa_Volume = VOLUME(RawAudio->ra_Volume);
  44.     ThrustAudioIO[1]->ioa_Cycles = 0;    /* infinite */
  45.  
  46.     BeginIO((struct IORequest *)ThrustAudioIO[0]);
  47.     BeginIO((struct IORequest *)ThrustAudioIO[1]);
  48. }
  49.  
  50. void FireAudio(struct Input *input)
  51. {
  52.     struct InputAudio *ia = &input->FireAudio;
  53.     struct IOAudio *ioa = ia->AudioIO;
  54.  
  55.     if (ia->Playing)
  56.     {
  57.     /* interrupt the audio already playing, to give a 'rapid fire' effect. */
  58.     GetMsg(ia->AudioMP);
  59.     ioa->ioa_Request.io_Command = ADCMD_FINISH;
  60.     ioa->ioa_Request.io_Flags = IOF_QUICK;
  61.     ioa->ioa_Request.io_Unit = (struct Unit *)ia->Unit;
  62.     ioa->ioa_AllocKey = ia->AllocKey;
  63.     BeginIO((struct IORequest *)ioa);
  64.     ia->Playing = FALSE;
  65.     }
  66.  
  67.     ioa->ioa_Request.io_Command = CMD_WRITE;
  68.     ioa->ioa_Request.io_Flags = ADIOF_PERVOL;
  69.     ioa->ioa_Request.io_Unit = (struct Unit *)ia->Unit;
  70.     ioa->ioa_AllocKey = ia->AllocKey;
  71.     ioa->ioa_Data = ia->AudioBuffer;
  72.     ioa->ioa_Length = ia->RawAudio->ra_SizeOfSample;
  73.     ioa->ioa_Period = ia->Period;
  74.     ioa->ioa_Volume = VOLUME(ia->RawAudio->ra_Volume);
  75.     ioa->ioa_Cycles = 1;
  76.     BeginIO((struct IORequest *)ioa);
  77.     ia->Playing = TRUE;
  78. }
  79.  
  80. ULONG InitAudio(struct IOAudio *FireAudioIO, struct IOAudio *ThrustAudioIO[])
  81. {
  82.     UBYTE FireAudioUnits[] = {1, 2, 4, 8};
  83.     /* Build the units required for stereo thruster audio, depending on the Audio
  84.      * channel given to the Fire audio.
  85.      */
  86.     UBYTE ThrustAudioUnits[][] = {{0, 0}, {0x2, 0x8}, {0x1, 0x4}, {0, 0}, {0x2, 0x1}, {0, 0}, {0, 0}, {0, 0}, {0x2, 0x1}};
  87.     ULONG FireUnit;
  88.     ULONG i;
  89.  
  90.     FireAudioIO->ioa_Data = FireAudioUnits;
  91.     FireAudioIO->ioa_Length = 4;
  92.     FireAudioIO->ioa_Request.io_Command = ADCMD_ALLOCATE;
  93.     FireAudioIO->ioa_Request.io_Flags = 0;
  94.     FireAudioIO->ioa_Request.io_Message.mn_Node.ln_Pri = 127;
  95.     BeginIO((struct IORequest *)FireAudioIO);
  96.     WaitPort(FireAudioIO->ioa_Request.io_Message.mn_ReplyPort);
  97.     GetMsg(FireAudioIO->ioa_Request.io_Message.mn_ReplyPort);
  98.     if (FireAudioIO->ioa_Request.io_Error != 0)
  99.     {
  100.     return(1);    /* show that Fire audio not allocated */
  101.     }
  102.  
  103.     for (i = 0; i < 2; i++)
  104.     {
  105.     FireUnit = (ULONG)FireAudioIO->ioa_Request.io_Unit;
  106.     ThrustAudioIO[i]->ioa_Data = &(ThrustAudioUnits[FireUnit][i]);
  107.     ThrustAudioIO[i]->ioa_Length = 1;
  108.     ThrustAudioIO[i]->ioa_Request.io_Command = ADCMD_ALLOCATE;
  109.     ThrustAudioIO[i]->ioa_Request.io_Flags = 0;
  110.     ThrustAudioIO[i]->ioa_Request.io_Message.mn_Node.ln_Pri = 127;
  111.     BeginIO((struct IORequest *)ThrustAudioIO[i]);
  112.     WaitPort(ThrustAudioIO[i]->ioa_Request.io_Message.mn_ReplyPort);
  113.     GetMsg(ThrustAudioIO[i]->ioa_Request.io_Message.mn_ReplyPort);
  114.     if (ThrustAudioIO[i]->ioa_Request.io_Error != 0)
  115.     {
  116.         return(2 + i);    /* show that Thrust audio not allocated */
  117.     }
  118.     }
  119.  
  120.     /* The caller may want to produce an error message to say which channel
  121.      * could not be allocated, hence the different error numbers returned.
  122.      */
  123.     return(0);
  124. }
  125.